package com.free.show;

import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * <p>
 * Title: Eclipse Plugin Development
 * </p>
 * <p>
 * Description: Free download
 * </p>
 * <p>
 * mail: ganshm@gmail.com
 * </p>
 */

public class MenuExample
{
    
    Display display;
    
    Shell shell;
    
    Menu menuBar, fileMenu, editMenu;
    
    MenuItem fileMenuHeader, editMenuHeader;
    
    MenuItem fileExitItem, fileSaveItem, fileEnglishItem, fileGermanItem, editCopyItem;
    
    Text text;
    
    public MenuExample()
    {
        display = new Display();
        shell = new Shell(display);
        shell.setText("Menu Example");
        shell.setSize(300, 200);
        shell.setText("菜单测试");
        InputStream resourceAsStream = getClass().getResourceAsStream("new.gif");
        Image createdImage = new Image(Display.getCurrent(), resourceAsStream);
        shell.setImage(createdImage); // 设置窗口图标
        text = new Text(shell, SWT.BORDER);
        text.setBounds(80, 50, 150, 25);
        
        // 添加主菜单项
        menuBar = new Menu(shell, SWT.BAR);
        // 添加一级子菜单
        fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE); // 文件级联菜单
        fileMenuHeader.setText("&File");
        
        // 添加一级子菜单的菜单项
        fileMenu = new Menu(shell, SWT.DROP_DOWN); // 添加下列菜单
        fileMenuHeader.setMenu(fileMenu);
        
        fileSaveItem = new MenuItem(fileMenu, SWT.PUSH);
        fileSaveItem.setText("&Save");
        
        fileEnglishItem = new MenuItem(fileMenu, SWT.RADIO);
        fileEnglishItem.setText("English");
        
        fileGermanItem = new MenuItem(fileMenu, SWT.RADIO);
        fileGermanItem.setText("German");
        
        fileExitItem = new MenuItem(fileMenu, SWT.PUSH);
        fileExitItem.setText("E&xit");
        
        editMenuHeader = new MenuItem(menuBar, SWT.CASCADE); // 编辑的级联菜单
        editMenuHeader.setText("&Edit");
        
        editMenu = new Menu(shell, SWT.DROP_DOWN);
        editMenuHeader.setMenu(editMenu);
        
        editCopyItem = new MenuItem(editMenu, SWT.PUSH);
        editCopyItem.setText("&Copy");
        
        // 添加菜单项的事件监听器
        fileExitItem.addSelectionListener(new MenuItemListener());
        fileSaveItem.addSelectionListener(new MenuItemListener());
        editCopyItem.addSelectionListener(new MenuItemListener());
        
        fileEnglishItem.addSelectionListener(new RadioItemListener());
        fileGermanItem.addSelectionListener(new RadioItemListener());
        
        shell.setMenuBar(menuBar);
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
    
    class MenuItemListener extends SelectionAdapter
    {
        public void widgetSelected(SelectionEvent event)
        {
            text.setText("You selected " + ((MenuItem)event.widget).getText());
            if (((MenuItem)event.widget).getText().equals("E&xit"))
            {
                shell.close();
            }
        }
    }
    
    class RadioItemListener extends SelectionAdapter
    {
        public void widgetSelected(SelectionEvent event)
        {
            MenuItem item = (MenuItem)event.widget;
            text.setText(item.getText() + " is on.");
        }
    }
    
    public static void main(String[] args)
    {
        MenuExample menuExample = new MenuExample();
    }
}
